package org.mspring.mlog.freemarker.directive;

import freemarker.core.Environment;
import freemarker.template.*;
import org.mspring.mlog.entity.Article;
import org.mspring.mlog.service.ArticleService;
import org.mspring.nbee.web.freemarker.DirectiveUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 热门文章
 *
 * @author Gao Youbo
 * @since 2015-01-24 09:58
 */
@Component
public class HotArticleDirectiveModel implements TemplateDirectiveModel {
    private ArticleService articleService;

    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        int limit = DirectiveUtils.getIntValue("limit", params);
        if (limit == 0) {
            limit = 8;
        }
        List<Article> articleList = articleService.listHotArticle(limit);
        env.setVariable("articleList", ObjectWrapper.DEFAULT_WRAPPER.wrap(articleList));
        body.render(env.getOut());
    }

    @Autowired
    public void setArticleService(ArticleService articleService) {
        this.articleService = articleService;
    }
}
